package com.masterclient.app.modules.income;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.base.CashCommonDialog;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.businessmodule.vipmodule.auth.UploadIDsActivity;
import com.masterclient.app.modules.income.contract.AccumulatedIncomeContract;
import com.masterclient.app.modules.income.contract.AccumulatedIncomePresenter;
import com.masterclient.app.modules.income.model.AccumulatedIncomeBean;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.uikit.header.MyRefreshHead;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

import static com.eoner.uikit.header.MyRefreshHead.LOTTIE;

@Route(path = AroutePath.ACCUMULATED_INCOME_FRAGMENT)
public class AccumulatedIncomeFragment extends BaseFragment<AccumulatedIncomePresenter> implements AccumulatedIncomeContract.View {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_bg)
    View ivBg;
    @BindView(R.id.tv_income_total)
    TextView tvIncomeTotal;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_bottom_tip)
    TextView tvBottomTip;
    @BindView(R.id.iv_hide_account)
    ImageView ivHideAccount;

    private AccumulatedIncomeBean accumulatedIncomeBean;
    private int mTopViewHeight = ScreenUtils.dp2px(370);//这个就是mTopView的高度
    private int mOffset = 0;
    private boolean isShow = true;

    public static AccumulatedIncomeFragment newInstance() {
        return new AccumulatedIncomeFragment();
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        setFullScreenAndTransparent(getActivity());
        return inflater.inflate(R.layout.fragment_accumulated_income, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        MyRefreshHead myRefreshHead = new MyRefreshHead(getActivity(), 1, LOTTIE);
        myRefreshHead.changeTextColor(Color.parseColor("#ffffff"));
        refreshLayout.setRefreshHeader(myRefreshHead);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getDealerData();
        });
        refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                mOffset = offset;
                if (ivBg != null){
                    ivBg.getLayoutParams().height = mTopViewHeight+mOffset;
                    ivBg.requestLayout();
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getDealerData();
    }

    @OnClick({R.id.rl_back, R.id.view_un_clear_account, R.id.view_unfreeze, R.id.view_income, R.id.view_back_card, R.id.tv_withdraw,
            R.id.view_hide_account, R.id.tv_issue, R.id.iv_issue})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                Objects.requireNonNull(getActivity()).finish();
                break;
            case R.id.view_un_clear_account:
                Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.UN_CLEAR_ACCOUNT_FRAGMENT)
                        .navigation();
                FragmentHostActivity.openFragment(getActivity(), fragment);
                break;
            case R.id.view_unfreeze:
                ARouter.getInstance().build(AroutePath.UNFREEZE_ACTIVITY)
                        .navigation();
                break;
            case R.id.view_income:
                ARouter.getInstance().build(AroutePath.INCOME_AND_EXPEND_ACTIVITY)
                        .navigation();
                break;
            case R.id.view_back_card:
                if (accumulatedIncomeBean != null && accumulatedIncomeBean.isIdentityCert){
                    ARouter.getInstance().build(AroutePath.DEALER_BACK_CARD_MANAGE_ACTIVITY)
                            .navigation();
                }else {
                    CashCommonDialog cashCommonDialog = new CashCommonDialog(mContext,"为保证您的资金账户安全，请上传您本人身份证用于账户安全保护","取消","去认证",0);
                    cashCommonDialog.setOnChooseListerner(new CashCommonDialog.OnChooseListerner() {
                        @Override
                        public void cancel() {
                            cashCommonDialog.dismiss();
                        }

                        @Override
                        public void confirm() {
                            Intent intent = new Intent(getActivity(), UploadIDsActivity.class);
                            intent.putExtra("pageType",1);
                            startActivity(intent);
                            cashCommonDialog.dismiss();
                        }
                    });
                    cashCommonDialog.show();
                }
                break;
            case R.id.tv_withdraw:
                if (accumulatedIncomeBean == null){
                    return;
                }
                if (!accumulatedIncomeBean.isIdentityCert || !accumulatedIncomeBean.isBankCert){
                    ToastUtil.showToast("未绑卡不能提现，请先绑卡");
                    return;
                }
                if (!accumulatedIncomeBean.withdrawEnable){
                    ToastUtil.showToast("本月提现次数已用完");
                    return;
                }
                if (getActivity() != null){
                    Fragment fragment3 = (Fragment) ARouter.getInstance().build(AroutePath.APPLY_WITHDRAW_FRAGMENT)
                            .navigation();
                    FragmentHostActivity.openFragment(getActivity(), fragment3);
                }
                break;
            case R.id.view_hide_account:
                if(isShow){
                    tvIncomeTotal.setText("****");
                    ivHideAccount.setImageResource(R.mipmap.icon_white_hide);
                    isShow = false;
                }else {
                    if (accumulatedIncomeBean != null){
                        tvIncomeTotal.setText(accumulatedIncomeBean.availableBalance);
                        Utils.setDin(tvIncomeTotal,getActivity());
                        ivHideAccount.setImageResource(R.mipmap.icon_white_show);
                        isShow = true;
                    }
                }
                SPUtils.getInstance().put(Config.ACCUMULATED_INCOME_IS_SHOW,isShow);
                break;
            case R.id.tv_issue:
            case R.id.iv_issue:
                if (accumulatedIncomeBean != null && !TextUtils.isEmpty(accumulatedIncomeBean.faqUrl)){
                    CommonUtil.goAnyWhere(getActivity(), accumulatedIncomeBean.faqUrl, "", "", "", "");
                }
                break;
        }
    }

    @Override
    public void showData(AccumulatedIncomeBean dataBean) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
        if (dataBean != null){
            accumulatedIncomeBean = dataBean;

            isShow = SPUtils.getInstance().getBoolean(Config.ACCUMULATED_INCOME_IS_SHOW,true);
            if(isShow){
                if (!TextUtils.isEmpty(dataBean.availableBalance)){
                    tvIncomeTotal.setText(dataBean.availableBalance);
                    Utils.setDin(tvIncomeTotal,getActivity());
                }
                ivHideAccount.setImageResource(R.mipmap.icon_white_show);
            }else {
                tvIncomeTotal.setText("****");
                ivHideAccount.setImageResource(R.mipmap.icon_white_hide);
            }

            if (!TextUtils.isEmpty(dataBean.availableWithdrawTips)){
                tvTip.setText(dataBean.availableWithdrawTips);
            }
            if (!TextUtils.isEmpty(dataBean.bankBindTips)){
                tvBottomTip.setText(dataBean.bankBindTips.replace("\\n", "\n"));
            }
            if (dataBean.isIdentityCert){
                if (dataBean.isBankCert){
                    tvBankCard.setText("查看");
                }else {
                    tvBankCard.setText("去绑卡");
                    tvBankCard.setTextColor(Color.parseColor("#E79235"));
                }
            }else {
                tvBankCard.setText("去绑卡");
                tvBankCard.setTextColor(Color.parseColor("#E79235"));
            }
        }
    }

    @Override
    public void showDataFailed(String msg) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh(false);
        }
        ToastUtil.showToast(msg);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
